﻿using System.Data;
using System.Text.RegularExpressions;
using System.IO;
using System.Text;

namespace YXShop.Common.TaoBaoAPI
{
  public  class TaobaoCommon
    {

      public TaobaoCommon()
      {
 
       }

      /// <summary>
      /// 根据淘宝提供的参数获取商品信息
      /// 默认设置1页20条数据
      /// </summary>
      /// <param name="appkey">淘宝提供的APP KEY</param>
      /// <param name="appsecret">淘宝提供的APP SECRET</param>
      /// <param name="pages">获取第几页数据</param>
      /// <param name="counts">获取数据总条数</param>
      /// <returns>跟商城产品数据库表一样架构的DataTable</returns>
      public DataTable GetTable(string appkey, string appsecret, int pages, ref long counts)
      {
          DataTable dtnew = new DataTable();
          try
          {
              DataColumn iid = new DataColumn("iid");
              dtnew.Columns.Add(iid);
              DataColumn pro_Property = new DataColumn("pro_Property");
              dtnew.Columns.Add(pro_Property);
              DataColumn pro_CID = new DataColumn("pro_CID");
              dtnew.Columns.Add(pro_CID);
              DataColumn pro_BrandID = new DataColumn("pro_BrandID");
              dtnew.Columns.Add(pro_BrandID);
              DataColumn pro_Name = new DataColumn("pro_Name");
              dtnew.Columns.Add(pro_Name);
              DataColumn pro_NO = new DataColumn("pro_NO");
              dtnew.Columns.Add(pro_NO);
              DataColumn pro_KeyWord = new DataColumn("pro_KeyWord");
              dtnew.Columns.Add(pro_KeyWord);
              DataColumn pro_ShopPrice = new DataColumn("pro_ShopPrice");
              dtnew.Columns.Add(pro_ShopPrice);
              DataColumn pro_MarketPrice = new DataColumn("pro_MarketPrice");
              dtnew.Columns.Add(pro_MarketPrice);
              DataColumn pro_RatingDiscount = new DataColumn("pro_RatingDiscount");
              dtnew.Columns.Add(pro_RatingDiscount);
              DataColumn pro_MetaDescription = new DataColumn("pro_MetaDescription");
              dtnew.Columns.Add(pro_MetaDescription);
              DataColumn pro_EPoints = new DataColumn("pro_EPoints");
              dtnew.Columns.Add(pro_EPoints);
              DataColumn pro_DonateIntegral = new DataColumn("pro_DonateIntegral");
              dtnew.Columns.Add(pro_DonateIntegral);
              DataColumn pro_Images = new DataColumn("pro_Images");
              dtnew.Columns.Add(pro_Images);
              DataColumn pro_Thumbnail = new DataColumn("pro_Thumbnail");
              dtnew.Columns.Add(pro_Thumbnail);
              DataColumn pro_Synopsis = new DataColumn("pro_Synopsis");
              dtnew.Columns.Add(pro_Synopsis);
              DataColumn pro_MetaKeywords = new DataColumn("pro_MetaKeywords");
              dtnew.Columns.Add(pro_MetaKeywords);
              DataColumn pro_MetaTitle = new DataColumn("pro_MetaTitle");
              dtnew.Columns.Add(pro_MetaTitle);
              DataColumn pro_Weight = new DataColumn("pro_Weight");
              dtnew.Columns.Add(pro_Weight);
              DataColumn pro_WeightUnit = new DataColumn("pro_WeightUnit");
              dtnew.Columns.Add(pro_WeightUnit);
              DataColumn pro_Unit = new DataColumn("pro_Unit");
              dtnew.Columns.Add(pro_Unit);
              DataColumn pro_Original = new DataColumn("pro_Original");
              dtnew.Columns.Add(pro_Original);
              DataColumn pro_Content = new DataColumn("pro_Content");
              dtnew.Columns.Add(pro_Content);
              DataColumn pro_Stock = new DataColumn("pro_Stock");
              dtnew.Columns.Add(pro_Stock);
              DataColumn pro_Designation = new DataColumn("pro_Designation");
              dtnew.Columns.Add(pro_Designation);
              DataColumn pro_AutoUp = new DataColumn("pro_AutoUp");
              dtnew.Columns.Add(pro_AutoUp);
              DataColumn pro_AutoDown = new DataColumn("pro_AutoDown");
              dtnew.Columns.Add(pro_AutoDown);
              DataColumn pro_Alarm = new DataColumn("pro_Alarm");
              dtnew.Columns.Add(pro_Alarm);
              DataColumn pro_AuditCount = new DataColumn("pro_AuditCount");
              dtnew.Columns.Add(pro_AuditCount);
              DataColumn pro_Audit = new DataColumn("pro_Audit");
              dtnew.Columns.Add(pro_Audit);
              DataColumn pro_State = new DataColumn("pro_State");
              dtnew.Columns.Add(pro_State);
              DataColumn pro_RelatingArtID = new DataColumn("pro_RelatingArtID");
              dtnew.Columns.Add(pro_RelatingArtID);
              DataColumn pro_RelatingproID = new DataColumn("pro_RelatingproID");
              dtnew.Columns.Add(pro_RelatingproID);
              DataColumn pro_CreateTime = new DataColumn("pro_CreateTime");
              dtnew.Columns.Add(pro_CreateTime);
              DataColumn pro_UpdateTime = new DataColumn("pro_UpdateTime");
              dtnew.Columns.Add(pro_UpdateTime);
              DataColumn pro_OnClicks = new DataColumn("pro_OnClicks");
              dtnew.Columns.Add(pro_OnClicks);
              DataColumn pro_IsAllowComment = new DataColumn("pro_IsAllowComment");
              dtnew.Columns.Add(pro_IsAllowComment);
              DataColumn pro_SaleNum = new DataColumn("pro_SaleNum");
              dtnew.Columns.Add(pro_SaleNum);
              DataColumn pro_CostPrice = new DataColumn("pro_CostPrice");
              dtnew.Columns.Add(pro_CostPrice);
              DataColumn pro_IsAuto = new DataColumn("pro_IsAuto");
              dtnew.Columns.Add(pro_IsAuto);
              DataColumn pro_SaleType = new DataColumn("pro_SaleType");
              dtnew.Columns.Add(pro_SaleType);
              DataColumn pro_IntegralPrice = new DataColumn("pro_IntegralPrice");
              dtnew.Columns.Add(pro_IntegralPrice);
              DataColumn pro_Area = new DataColumn("pro_Area");
              dtnew.Columns.Add(pro_Area);
              DataColumn pro_PutoutID = new DataColumn("pro_PutoutID");
              dtnew.Columns.Add(pro_PutoutID);
              DataColumn pro_PutoutType = new DataColumn("pro_PutoutType");
              dtnew.Columns.Add(pro_PutoutType);
              DataColumn pro_Templates = new DataColumn("pro_Templates");
              dtnew.Columns.Add(pro_Templates);
              DataColumn imgs = new DataColumn("imgs");
              dtnew.Columns.Add(imgs);


              TopXmlRestClient client = new TopXmlRestClient("http://gw.api.taobao.com/router/rest", appkey.Trim(), appsecret.Trim());
              ItemsAllGetRequest request = new ItemsAllGetRequest();
              request.Fields = "iid";
              request.PageNo = pages;
              request.PageSize = 20;
              PageList<Item> item = client.ItemsAllGet(request);
              counts = item.TotalResults;

              foreach (Item tem in item.Content)
              {
                  ItemGetRequest request1 = new ItemGetRequest();
                  request1.Fields = "props,title,item_img,num_iid,price,pic_url,desc,num,type,list_time ,delist_time,approve_status,modified";
                  request1.Iid = tem.Iid;
                  Item it1 = client.ItemGet(request1);
                  DataRow dr = dtnew.NewRow();

                  #region 商品信息
                  dr["iid"] = tem.Iid;
                  string imgsurl = "";
                  if (it1.ItemImgs.Count > 0)
                  {
                      foreach (ItemImg ii in it1.ItemImgs)
                      {
                          imgsurl = imgsurl + "," + ii.Url;
                      }
                      dr["imgs"] = imgsurl;
                  }
                  dr["pro_Property"] = string.Empty;
                  dr["pro_CID"] = string.Empty;
                  dr["pro_BrandID"] = 0;
                  dr["pro_Name"] = it1.Title;
                  dr["pro_NO"] = it1.NumIid;
                  dr["pro_KeyWord"] = string.Empty;
                  dr["pro_ShopPrice"] = it1.Price;
                  dr["pro_MarketPrice"] = it1.Price;
                  dr["pro_RatingDiscount"] = string.Empty;
                  dr["pro_MetaDescription"] = string.Empty;
                  dr["pro_EPoints"] = 0;
                  dr["pro_DonateIntegral"] = 0;
                  dr["pro_Original"] = it1.PicUrl;
                  dr["pro_Images"] = string.Empty;
                  dr["pro_Thumbnail"] = string.Empty;
                  dr["pro_Synopsis"] = it1.Title;
                  dr["pro_Content"] = it1.Desc;
                  dr["pro_MetaKeywords"] = it1.Title;
                  dr["pro_MetaDescription"] = it1.Title;
                  dr["pro_MetaTitle"] = it1.Title;
                  dr["pro_Weight"] = 1;
                  dr["pro_WeightUnit"] = string.Empty;//默认为千克                
                  dr["pro_Stock"] = it1.Num;
                  dr["pro_Unit"] = string.Empty;
                  dr["pro_Alarm"] = 1;
                  dr["pro_AuditCount"] = 0;
                  dr["pro_Audit"] = 0;//默认为下架 因为批量导入的不是完整信息
                  dr["pro_State"] = "0|0|0|0|0|0|";
                  if (it1.Type == "auction")
                  {
                      dr["pro_Designation"] = "1|0|1|0|0|";
                  }
                  else
                  {
                      dr["pro_Designation"] = "1|0|0|0|0|";
                  }
                  dr["pro_RelatingArtID"] = "0";
                  dr["pro_RelatingproID"] = "0";
                  dr["pro_CreateTime"] = it1.Modified;
                  dr["pro_UpdateTime"] = it1.Modified;
                  dr["pro_OnClicks"] = 0;
                  dr["pro_IsAllowComment"] = 1;
                  dr["pro_SaleNum"] = 0;
                  dr["pro_CostPrice"] = 0;
                  dr["pro_AutoUp"] = it1.ListTime;
                  dr["pro_AutoDown"] = it1.DelistTime;
                  dr["pro_IsAuto"] = it1.ApproveStatus;
                  dr["pro_SaleType"] = 0;
                  dr["pro_IntegralPrice"] = 0;
                  dr["pro_Area"] = "0";
                  dr["pro_PutoutID"] = 0;
                  dr["pro_PutoutType"] = 0;
                  dr["pro_Templates"] = "";
                  #endregion
                  dtnew.Rows.Add(dr);

              }
          }
          catch
          {
              dtnew = null;
          }
          return dtnew;
      }


    }



}
